package com.north.light.androidutils.recyclerview.scroll;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;


/**
 * FileName: DistanceRecyclerView
 * Author: lzt
 * Date: 2022/8/8 10:38
 * 滑动距离监听recyclerview
 */
public class DistanceRecyclerView extends RecyclerView {
    private ScrollOffsetListener mListener;

    public DistanceRecyclerView(@NonNull Context context) {
        super(context);
        init();
    }

    public DistanceRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DistanceRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //竖直方向-------------------------------------------------
                int verticalExtent = recyclerView.computeVerticalScrollExtent();
                int verticalOffset = recyclerView.computeVerticalScrollOffset();
                int verticalTotal = recyclerView.computeVerticalScrollRange();

                //水平方向-------------------------------------------------
                int horizontalExtent = recyclerView.computeHorizontalScrollExtent();
                int horizontalOffset = recyclerView.computeHorizontalScrollOffset();
                int horizontalTotal = recyclerView.computeHorizontalScrollRange();

                //监听回调--------------------------------------------------
                try {
                    if (mListener != null) {
                        //竖直
                        mListener.verticalEvent(verticalOffset, verticalOffset + verticalExtent, verticalTotal,
                                (((verticalOffset + verticalExtent) * 1.0f) / (verticalTotal * 1.0f)));
                        //水平
                        mListener.horizontalEvent(horizontalOffset, horizontalOffset + horizontalExtent, horizontalTotal,
                                (((horizontalOffset + horizontalExtent) * 1.0f) / (horizontalTotal * 1.0f)));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }


    //外部监听
    public void setScrollOffsetListener(ScrollOffsetListener scrollOffsetListener) {
        this.mListener = scrollOffsetListener;
    }

    public void removeScrollOffsetListener() {
        this.mListener = null;
    }

    public interface ScrollOffsetListener {

        public void verticalEvent(int offset, int progress, int duration, float percent);

        public void horizontalEvent(int offset, int progress, int duration, float percent);
    }
}
